package com.common.dingding.controller;

import com.common.dingding.entity.Book;
import com.common.dingding.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书控制器
 * 提供RESTful API接口实现增删改查功能
 */
@RestController
@RequestMapping("/api/books")
@Tag(name = "图书管理", description = "图书的增删改查接口")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 根据ID获取图书
     * @param id 图书ID
     * @return 图书对象
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取图书", description = "通过图书ID查询单本图书信息")
    public ResponseEntity<Book> getBookById(@PathVariable @Parameter(description = "图书ID") Long id) {
        Book book = bookService.getById(id);
        if (book != null) {
            return ResponseEntity.ok(book);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 获取所有图书
     * @return 图书列表
     */
    @GetMapping
    @Operation(summary = "获取所有图书", description = "查询所有图书信息列表")
    public ResponseEntity<List<Book>> getAllBooks() {
        List<Book> books = bookService.listAll();
        return ResponseEntity.ok(books);
    }

    /**
     * 新增图书
     * @param book 图书对象
     * @return 创建的图书
     */
    @PostMapping
    @Operation(summary = "新增图书", description = "添加一本新的图书信息")
    public ResponseEntity<Book> createBook(@RequestBody @Parameter(description = "图书信息") Book book) {
        boolean saved = bookService.save(book);
        if (saved) {
            return ResponseEntity.ok(book);
        } else {
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 更新图书
     * @param id 图书ID
     * @param book 图书对象
     * @return 更新后的图书
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新图书", description = "更新指定ID的图书信息")
    public ResponseEntity<Book> updateBook(@PathVariable @Parameter(description = "图书ID") Long id, @RequestBody @Parameter(description = "图书信息") Book book) {
        // 确保更新的是指定ID的图书
        Book bookUpdate = new Book();
        bookUpdate.setId(id);
        bookUpdate.setName(book.getName());
        bookUpdate.setPrice(book.getPrice());
        boolean updated = bookService.updateById(bookUpdate);
        if (updated) {
            return ResponseEntity.ok(book);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 删除图书
     * @param id 图书ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除图书", description = "删除指定ID的图书")
    public ResponseEntity<Void> deleteBook(@PathVariable @Parameter(description = "图书ID") Long id) {
        boolean deleted = bookService.removeById(id);
        if (deleted) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}